<template>
  <div class="outside">
    <header>
        <div class="left">
          <span class="title">外勤打卡</span>
          <span class="iconfont icon-xihuan"></span>
          <span class="iconfont icon-xiugai"></span>
          <span class="iconfont icon-gengduo"></span>
        </div>
        <div class="right">
          <div class="img-box">
            <img src="../../assets/tx.jpg">
          </div>
          <span>{{this.userinfo.name}}</span>
        </div>
    </header>

     <!-- 第二行 -->
    <div class="subnav">
        <el-button size="small" type="primary" @click="drawer = true">发起新的</el-button>
        <el-button size="small">  
          <el-upload
            :show-file-list="false"
            :on-success="onSuccess"
            class="upload"
            :headers="headers"
            :data="{userid:userinfo._id}"
            name="excel"
            action="/api/outside/import">导入
          </el-upload>
        </el-button>
        <!-- 标签页 -->
        <span class="tab">
          <span :class="{active:currentIndex ==index}" v-for="(item,index) in tabs" :key="index" @click="toggle(index)">{{item}}</span>
        </span>
    </div>

     <!-- 第三行 -->
    <div class="opelist">
      <a :href="'/api/outside/download?userid='+userinfo._id">
        <div class="ope" >
        <i class="el-icon-position"></i>
        <span>导出</span>
        </div>
      </a>
      <div class="ope">
       <i class="el-icon-setting"></i>
       <span>字段管理</span>
      </div>
      <div class="ope">
      <i class="el-icon-full-screen"></i>
       <span>精简试图</span>
      </div>
      <div class="ope">
      <i class="el-icon-search"></i>
       <span>搜索</span>
      </div>
    </div>

     <!-- 表格 -->
    <el-table 
     v-if="currentIndex!=6"
    :data="newoutsidelist"
    style="width: 100%">
      <el-table-column
        prop="_id"
        label="编号"
        align="center"
        show-overflow-tooltip
        width="120">
      </el-table-column>
      <el-table-column
        prop="status"
        label="当前流程状态"
        :formatter="formater_status"
        align="center">
      </el-table-column>
      <el-table-column
        prop="name"
        label="打卡人"
        align="center">
      </el-table-column>
      <el-table-column
        prop="checktype"
        label="打卡类型"
        :formatter="formater_checktype"
        align="center">
      </el-table-column>
      <el-table-column
        prop="checktime"
        label="打卡时间"
        :formatter="formater_time"
        align="center">
      </el-table-column>
      <el-table-column
        prop="time"
        label="申请时间"
        :formatter="formater_time"
        align="center">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center">
        <template slot-scope="scope">
        <!-- scope.row 当前行的 数据对象 -->
        <!-- scope.$index 当前行的 数据对象的索引 -->
          <el-button
            @click.native.prevent="deleteRow(scope.row._id)"
            type="danger"
            size="small">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 绘制图表的位置 -->
    <MyEchart v-if="currentIndex==6"></MyEchart>

    <!-- 分页 -->
    <el-pagination
      v-if="currentIndex!=6"
      class="pagination"
      background
      layout="prev, pager, next"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>

    <!-- 抽屉信息 -->
    <div class="drawer" v-show="drawer == true" @click="drawer = false">
      <div class="form-box" @click.stop="">
        <div class="title">外勤打卡</div>
        <div class="content">
          <h2>个人信息</h2>
          <div class="appli-box">
            <div class="applicant">
              <div class="a-title">打卡人</div>
              <input type="text" v-model="formDate.name">
            </div>
            <div class="bumen">
              <div class="a-title">所在部门</div>
              <input type="text">
            </div>
          </div>
          <div class="a-title">打卡类型</div>
          <el-select v-model="formDate.checktype"  placeholder="请选择">
            <el-option
              label="正常打卡"
              value="0">
            </el-option>
            <el-option
              label="补卡"
              value="1">
            </el-option>
          </el-select>
          <div class="checktime">
            <div class="a-title">打卡时间</div>
            <el-date-picker
              v-model="formDate.checktime"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
          <div class="checktime">
            <div class="a-title">申请时间</div>
            <el-date-picker
              v-model="formDate.time"
              type="datetime"
              placeholder="选择日期时间">
            </el-date-picker>
          </div>
          <div class="btn" @click="submitForm"> 
            <span>提&nbsp;交</span>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { mapState } from 'vuex'
import {outside_add,outside_list,outside_delete} from '../../utils/api'
import MyEchart from '../../components/MyEchart.vue'
export default {
  data() {
    return {
      drawer: false,
      currentIndex:0,
      total:0,//出差申请总数
      page:1,//页码
      headers:{Authorization:'Bearer ' + localStorage.getItem('token')},
      tabs:["待办事项","已办事项","抄送事项","我发起的","全部数据","概览","统计报表"],
      formDate:{
          name:this.$store.state.userinfo.name,
          checktype:'',
          checktime:'',
          time:'',
      },
      outsidelist:[],
      newoutsidelist:[]
    };
  },
  components:{
    MyEchart
  },
  mounted(){
   outside_list({ userid: this.userinfo._id ,page:1 }).then((res)=>{
    this.outsidelist = res.data.list;
    this.total = res.data.total;
    this.toggle(this.currentIndex)
    })
  },
  computed:{
    ...mapState(['userinfo'])
  },
  methods:{
    // 添加出差申请
    submitForm(){
      console.log(this.formDate);
     outside_add( {userid: this.userinfo._id ,...this.formDate }).then((res)=>{
        console.log(res);
        if( res.data.code == 200 ){
          this.outside = res.data.list;
          this.total = res.data.total;
          this.$message.success(res.data.msg);
          this.drawer = false;//隐藏抽屉
          }else{
            this.$message.error(res.data.msg);
        }
      })
    },
    // 删除
    deleteRow(id){
      //删除对应id的 外勤打卡
      outside_delete({userid: this.userinfo._id , id ,page: this.page }).then((res)=>{
        this.outsidelist = res.data.list;
        this.total = res.data.total;
        this.$message.success(res.data.msg);
      })
    },

    // 点击不同功能实现不同的效果
    toggle(index){
      this.currentIndex=index
      if(index==0){
        this.myForEach()
        this.newoutsidelist=this.newoutsidelist.filter((item)=>{
          return  item.status=='0'
        })
      }
      else if(index==1){
        this.myForEach()
        this.newoutsidelist=this.newoutsidelist.filter((item)=>{
          return  item.status=='1'
        })
      }
      else if(index==3){
        this.myForEach()
        this.newoutsidelist=this.newoutsidelist.filter((item)=>{
          return  item.name==this.userinfo.name 
        })
      }
      else if(index==4){
         this.newoutsidelist=this.outsidelist
      }
    },
    // 遍历数组sidelist
    myForEach(){
      this.outsidelist.forEach((item,index)=>{
        this.newoutsidelist[index]=item
      }) 
    },

    // 分页
    handleCurrentChange(val){
      this.page = val; //保存页码
      //请求对应页码的数据
      outside_list({ userid: this.userinfo._id ,page:val }).then((res)=>{
        this.outsidelist = res.data.list;
        this.toggle(this.currentIndex)
      })
    },    
    // 上传文件
    onSuccess(res){
      if( res.code == 200 ){
        this.$message.success(res.msg);
      }else{
        this.$message.error(res.msg);
      }
    },
    // 格式化处理
    formater_status(row, column, cellValue, index){
      return cellValue == 0 ? '待审批' : '已通过';
    },
    formater_checktype(row, column, cellValue, index){
      return cellValue == 0 ? '正常打卡' : '补卡';
    },
    formater_num(num){
        return num < 10 ? '0'+num : num;
    },
    formater_time(row, column, cellValue, index){
        var d = new Date( cellValue );
        return `${d.getFullYear()}-${ this.formater_num( d.getMonth()+1 ) }-${ this.formater_num( d.getDate() ) } ${this.formater_num( d.getHours())}:${this.formater_num( d.getMinutes())}:${this.formater_num( d.getSeconds() )} ` ;
    },
  }
};
</script>

<style lang="scss" scoped>
.outside{
  height: 100%;
  header{
    height: 60px;
    padding: 0 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #fff;
    border-bottom: 1px solid #e7ebf0;
    .left{
      span{
      font-size: 20px;
      margin-right: 24px;
      }
      .title{
        font-weight: 700;
      }
      .icon-xihuan{
        color: #fb9337;
      }
    }
    .right{
      display: flex;
      align-items: center;
      .img-box{
        width: 35px;
        height: 35px;
        margin-right: 16px;
        border-radius: 50%;
        background-color: #fb8924;
        img{
          width: 100%;
          border-radius: 50%;
        }
      }
    }
  }
  .subnav{
    padding-left: 24px;
    height: 60px;
    line-height: 60px;
    background-color: #fff;
    border-bottom: 1px solid #e7ebf0;
    .tab{
      line-height: 60px;
      span{
        display: inline-block;
        height: 60px;
        font-size: 14px;
        color: #767e89;
        cursor: pointer;
        box-sizing: border-box;
        margin: 0 18px;
        margin-left: 40px;
        border-bottom: 3px solid transparent;
      }
      span.active{
        color: #000;
        border-bottom: 3px solid #fb8924;
      }
    }
  }
  .opelist{
    height: 55px;
    margin: 12px 0 12px 24px;
    background-color: #fff;
    display: flex;
    align-items: center;
    .ope{
      padding:0 12px;
      cursor: pointer;
      color: #2a3341;
      border-right: 1px solid #eaeaef;
      i{
      margin-right: 6px;
      }
    }
    
  }
  .el-table{
    height: 400px;
    width: calc(100% - 24px);
    margin-left: 24px;
    overflow-y: scroll;
  }
  .el-table::before{
    height: 0;
  }
}
// 分页
.pagination{
  margin-top: 30px;
  display: flex;
  justify-content: center;
}
 // 抽屉
 .drawer{
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.6);
  .form-box{
    width: 40%;
    height: 100%;
    padding: 0 26px;
    background-color: #f9fafc;
    position: absolute;
    right: 0;
    .title{
       height: 60px;
       line-height: 60px;
       font-weight: 700;
       padding-left: 16px;
       margin-bottom: 12px;
       background-color: #fff;
    }
    .content{
      padding: 16px;
      background-color: #fff;
      input{
        width: 100%;
        height: 36px;
        box-sizing: border-box;
        padding-left: 12px;
        border: 1px solid #ccc;
      }
      .a-title{
        font-size: 14px;
        font-weight: 700;
        height: 30px;
        line-height: 30px;
      }
      .appli-box{
        margin: 20px 0;
        display: flex;
        .applicant{
          width: 300px;
          margin-right: 24px;
        }
        .bumen{
          width: 200px;
        }
      }
      .checktime{
        margin: 20px 0;
        .el-date-editor.el-input{
          width: 90%;
        }
      }
      .el-select{
        width: 90%;
      }
      .btn{
        height: 40px;
        width: 100px;
        color: #fff;
        cursor: pointer;
        text-align: center;
        line-height: 40px;
        font-size: 18px;
        margin: 30px auto 0;
        background-color: #fb9337;
      }
    }
  }
 }

</style>